<template>
  <div class="register">
    <div class="el-login-biaoti">
      <div class="biaoti-content">
        <div>
          <img src="@/assets/images/HomeLogo.png">
        </div>
        <div>
          <div class="biaoti-samll">在线交通信息一体化平台</div>
          <div class="biaoti-big">新区交通综合信息服务系统</div>
<!--          <div class="biaoti-samll">www.wangzhi.gov.com</div>-->
        </div>
      </div>
    </div>
    <div>
      <el-form ref="registerForm" :model="registerForm" :rules="registerRules" class="register-form" label-width="80px" label-position="left">
        <h3 class="title">密码找回</h3>
        <el-form-item prop="email" label="邮箱">
          <el-input
            v-model="registerForm.email"
            type="text"
            auto-complete="off"
            placeholder="邮箱"
            @keyup.enter.native="handleRegister"
          />
        </el-form-item>
        <el-form-item prop="code" label="验证码">
          <el-input
            v-model="registerForm.code"
            auto-complete="off"
            placeholder="验证码"
            style="width: 66%"
            @keyup.enter.native="handleRegister"
          >
            <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
          </el-input>
          <el-button class="register-code">
            <div v-if="show == 0" @click="getTestCode">获取验证码</div>
            <div v-if="show == 1">已发送{{ count }}秒</div>
            <div v-if="show == 2" @click="getTestCode">重新获取</div>
          </el-button>
        </el-form-item>
        <el-form-item prop="newPassword" label="新密码">
          <el-input
            v-model="registerForm.newPassword"
            type="password"
            auto-complete="off"
            placeholder="新密码"
            @keyup.enter.native="handleRegister"
          >
            <!--          <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />-->
          </el-input>
        </el-form-item>
        <el-form-item prop="repeatPassword" label="重复密码">
          <el-input
            v-model="registerForm.repeatPassword"
            type="password"
            auto-complete="off"
            placeholder="重复密码"
            @keyup.enter.native="handleRegister"
          >
            <!--          <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />-->
          </el-input>
        </el-form-item>
        <div style="width:100%;">
          <el-button
            :loading="loading"
            size="medium"
            type="primary"
            style="width:100%;"
            @click.native.prevent="find"
          >
            找回
          </el-button>
          <div style="float: right; margin: 12px 0px 22px 0px">
            <router-link class="link-type" :to="'/login'">使用已有账户登录</router-link>
          </div>
        </div>
      </el-form>
    </div>
    <!--  底部  -->
    <!--    <div class="el-register-footer">-->
    <!--      <span>Copyright © 2018-2023 ruoyi.vip All Rights Reserved.</span>-->
    <!--    </div>-->
  </div>
</template>

<script>
import { getCodeImg, getPasswordCode, register, retrievePassword } from '@/api/login'

export default {
  name: 'Register',
  data() {
    const equalToPassword = (rule, value, callback) => {
      if (this.registerForm.newPassword !== value) {
        callback(new Error('两次输入的密码不一致'))
      } else {
        callback()
      }
    }
    return {
      show: 0,
      count: '',
      timer: null,
      codeUrl: '',
      registerForm: {
        newPassword: '',
        repeatPassword: '',
        email: '',
        code: ''
      },
      registerRules: {
        email: [
          { required: true, trigger: 'blur', message: '请输入您的邮箱' }
        ],
        newPassword: [
          { required: true, trigger: 'blur', message: '请输入您的密码' },
          { min: 5, max: 20, message: '用户密码长度必须介于 5 和 20 之间', trigger: 'blur' }
        ],
        repeatPassword: [
          { required: true, trigger: 'blur', message: '请再次输入您的密码' },
          { min: 5, max: 20, message: '用户密码长度必须介于 5 和 20 之间', trigger: 'blur' },
          { required: true, validator: equalToPassword, trigger: 'blur' }
        ],
        code: [{ required: true, trigger: 'change', message: '请输入验证码' }]
      },
      loading: false,
      captchaEnabled: true
    }
  },
  created() {
    this.getCode()
  },
  methods: {
    getTestCode() {
      // if (this.registerForm.email !== '') {
      //   getPasswordCode({ email: this.registerForm.email }).then(res => {
      //     this.$message('发送成功')
      //   })
      // } else {
      //   this.$message('邮箱不能为空')
      // }
      const times = 60 // 倒计时时间
      if (this.registerForm.email !== '') {
        getPasswordCode({ email: this.registerForm.email }).then(res => {
          if (res.code === 200) {
            this.$message.success('发送成功')
            if (!this.timer) {
              this.count = times
              this.show = 1
              this.timer = setInterval(() => {
                if (this.count > 0 && this.count <= times) {
                  this.count--
                } else {
                  this.show = 2
                  clearInterval(this.timer)
                  this.timer = null
                }
              }, 1000)
            }
          }
        })
      } else {
        this.$message.error('邮箱不能为空')
      }

      // const times = 60 // 倒计时时间
      // if (!this.timer) {
      //   this.count = times
      //   this.show = 1
      //   this.timer = setInterval(() => {
      //     if (this.count > 0 && this.count <= times) {
      //       this.count--
      //     } else {
      //       this.show = 2
      //       clearInterval(this.timer)
      //       this.timer = null
      //     }
      //   }, 1000)
      // }
    },
    find() {
      // retrievePassword(this.registerForm).then(res => {
      //   this.$message('成功')
      // })
      this.$refs.registerForm.validate(valid => {
        if (valid) {
          this.loading = true
          retrievePassword(this.registerForm).then(res => {
            this.$alert("<font color='red'>恭喜你，您的密码修改成功", '系统提示', {
              dangerouslyUseHTMLString: true,
              type: 'success'
            }).then(() => {
              this.$router.push('/login')
            }).catch(() => {})
          }).catch(() => {
            this.loading = false
            // if (this.captchaEnabled) {
            //   this.getCode()
            // }
          })
        }
      })
    },
    // getCode() {
    //   getCodeImg().then(res => {
    //     this.captchaEnabled = res.captchaEnabled === undefined ? true : res.captchaEnabled
    //     if (this.captchaEnabled) {
    //       this.codeUrl = 'data:image/gif;base64,' + res.img
    //       this.registerForm.uuid = res.uuid
    //     }
    //   })
    // },
    handleRegister() {
      this.$refs.registerForm.validate(valid => {
        if (valid) {
          this.loading = true
          register(this.registerForm).then(res => {
            const username = this.registerForm.username
            this.$alert("<font color='red'>恭喜你，您的账号 " + username + ' 注册成功！</font>', '系统提示', {
              dangerouslyUseHTMLString: true,
              type: 'success'
            }).then(() => {
              this.$router.push('/login')
            }).catch(() => {})
          }).catch(() => {
            this.loading = false
            if (this.captchaEnabled) {
              this.getCode()
            }
          })
        }
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.register {
  display: flex;
  justify-content: right;
  align-items: center;
  height: 100%;
  background-image: url("../assets/images/Loginbanner.png");
  background-size: cover;
  .el-login-biaoti {
    height: 40px;
    line-height: 40px;
    position: fixed;
    top: 0;
    width: 100%;
    text-align: center;
    color: #fff;
    font-family: Arial;
    font-size: 12px;
    letter-spacing: 1px;
    .biaoti-content {
      padding: 0px 200px;
      width: 100%;
      background-color: #ffffff;
      display: flex;
      flex-direction: row;
      justify-content: left;
      align-items: flex-start;
      img {
        width: 100px; height: 120px
      }
      .biaoti-samll {
        color: #333333;
        font-size: 14px;
        text-align: left;
      }
      .biaoti-big {
        color: #333333;
        font-weight: bold;
        font-size: 32px;
        text-align: left;
      }
    }
  }
}

.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #707070;
}

.register-form {
  border-radius: 6px;
  background: #ffffff;
  width: 440px;
  height: 410px;
  padding: 25px 25px 5px 25px;
  margin-right: 200px;
  .el-input {
    height: 38px;
    input {
      height: 38px;
    }
  }
  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}
.register-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}
.register-code {
  width: 33%;
  height: 38px;
  float: right;
  img {
    cursor: pointer;
    vertical-align: middle;
  }
}
.el-register-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}
.register-code-img {
  height: 38px;
}
</style>
